package com.example.fina2202.day09;

public class BinarySear {
    public static void main(String[] args) {
        int[] ary={98,87,65,54,43,32,21,10,8,7,0};
        int target = 100;
        int index = search(ary,target);
        System.out.println(index);
    }

    //根据元素查询对应的下标
    public static int search(int[] ary,int target){
        int start=0,end=ary.length-1,middle;
        while (start<=end){
            //确定中间位置的下标
            middle = (end-start)/2+start;
            //比较middle的元素与目标元素是否相等
            if (target==ary[middle])
                return middle;
            else if(target>ary[middle])
                end = middle-1;
            else
                start = middle+1;
        }
        return -1;
    }

}
